package com.demo.myapplication.utils;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.SimpleAdapter;

import com.demo.myapplication.R;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MultiChoiceListener implements AbsListView.MultiChoiceModeListener {
  private final AbsListView absListView;
  private Activity activity;
  private List<Map<String, Object>> dataList;

  public MultiChoiceListener setDataList(List<Map<String, Object>> dataList) {
    this.dataList = dataList;
    return this;
  }

  public MultiChoiceListener(AbsListView absListView, Activity activity) {
    this.absListView = absListView;
    this.activity = activity;
  }

  @Override
  public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
    int checkedCount = absListView.getCheckedItemCount();
    if (checkedCount == 0) {
      mode.setSubtitle(null);
    } else {
      mode.setSubtitle(String.format(activity.getString(R.string.choosed_item), checkedCount));
    }
  }

  @Override
  public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    activity.getMenuInflater().inflate(R.menu.fragment_actionmode_menu, menu);
    mode.setTitle(activity.getString(R.string.choose_item));
    return true;
  }

  @Override
  public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false;
  }

  @Override
  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    SimpleAdapter adapter = (SimpleAdapter) absListView.getAdapter();
    SparseBooleanArray checked = absListView.getCheckedItemPositions();
    switch (item.getItemId()) {
      case R.id.delete:
        if (checked != null) {
          for (int i = checked.size() - 1; i >= 0; i--) {
            if (checked.valueAt(i)) {
              dataList.remove(checked.keyAt(i));
            }
          }
        }
        adapter.notifyDataSetChanged();
        break;
      case R.id.download:
        StringBuilder name = new StringBuilder("[");
        if (checked != null) {
          for (int i = 0; i < checked.size(); i++) {
            if (checked.valueAt(i)) {
              Map<String, Object> map = (Map<String, Object>) adapter.getItem(checked.keyAt(i));
              String text = (String) map.get(CommonUtils.map_text_key);
              name.append(text).append(",");
            }
          }
        }
        name.deleteCharAt(name.length() - 1);
        name.append("]");
        Snackbar.make(absListView, String.format(activity.getString(R.string.tip_download), name.toString()), Snackbar.LENGTH_LONG).show();
        break;
    }

    mode.finish();
    return true;
  }

  @Override
  public void onDestroyActionMode(ActionMode mode) {
    SparseBooleanArray checked = absListView.getCheckedItemPositions();
    if (checked != null) {
      for (int i = checked.indexOfValue(true); i >= 0; i = checked.indexOfValue(true)) {
        absListView.setItemChecked(checked.keyAt(i), false);
      }
    }
  }
}